/*
 * @Author: 张久冬
 * @Date: 2021-12-29 14:27:33
 * @
# Description:
 */
import { Module } from 'vuex'
import type { ISystemState } from './type'
import { IRootState } from '../../type'
import { getPageListData } from '@/service/main/system/system'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      // user的数据
      userList: [],
      userCount: 0,

      // role的数据
      roleList: [],
      roleCount: 0,

      // goods的数据
      goodList: [],
      goodCount: 0,

      // menus的数据
      menusList: [],
      menusCount: 0
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        switch (pageName) {
          case 'User':
            return state.userList
            break
          case 'Role':
            return state.roleList
            break
          case 'Goods':
            return state.goodList
            break
          case 'Menus':
            return state.menusList
            break
        }
      }
    },
    pageDataCount(state) {
      return (pageName: string) => {
        switch (pageName) {
          case 'User':
            return state.userCount
            break
          case 'Role':
            return state.roleCount
            break
          case 'Goods':
            return state.goodCount
            break
          case 'Menus':
            return state.menusCount
            break
        }
      }
    }
  },
  mutations: {
    // user的mutations
    changeUserList(state, userList: any[]) {
      state.userList = userList
    },
    changeUserCount(state, userCount: number) {
      state.userCount = userCount
    },
    // role的mutations
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    // goods的mutations
    changeGoodsList(state, goodList: any[]) {
      state.goodList = goodList
    },
    changeGoodsCount(state, goodCount: number) {
      state.goodCount = goodCount
    },
    // goods的mutations
    changeMenusList(state, menusList: any[]) {
      state.menusList = menusList
    },
    changeMenusCount(state, menusCount: number) {
      state.menusCount = menusCount
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 获取pageUrl
      const { queryInfo, pageName } = payload
      let pageUrl
      switch (pageName) {
        case 'User':
          pageUrl = '/users/list'
          break
        case 'Role':
          pageUrl = '/role/list'
          break
        case 'Goods':
          pageUrl = '/goods/list'
          break
        case 'Menus':
          pageUrl = '/menu/list'
          break
      }

      // 对页面发送请求
      const PageList = await getPageListData(pageUrl, queryInfo)

      // 保存请求结果
      const { list, totalCount } = PageList.data
      console.log(`菜单${pageName}请求list数据为`, list)
      console.log(`菜单${pageName}请求totalCount数据为`, totalCount)

      switch (pageName) {
        case 'User':
          commit('changeUserList', list)
          commit('changeUserCount', totalCount)
          break
        case 'Role':
          commit('changeRoleList', list)
          commit('changeRoleCount', totalCount)
          break
        case 'Goods':
          commit('changeGoodsList', list)
          commit('changeGoodsCount', totalCount)
          break
        case 'Menus':
          commit('changeMenusList', list)
          commit('changeMenusCount', totalCount)
          break
      }
    }
  }
}
export default systemModule
